export default {
  getEvents() {
    return {
      keyup: 'onKeyUp',
      keydown: 'onKeyDown',
      // 进入画布
      'canvas:mouseenter': 'onCanvasMouseenter',
      // 离开画布
      'canvas:mouseout': 'onCanvasMouseout'
    };
  },
  onKeyDown(e) {
    const keyboard = {
      del: 46,
      back: 8,
      ctrlZ: 'ctrl+z',
      ctrlY: 'ctrl+y'
    };
    if (!this.isKeyboardEnabled) return;
    let code = e.keyCode || e.which;
    e.ctrlKey && code === 90 && (code = 'ctrl+z');
    e.ctrlKey && code === 89 && (code = 'ctrl+y');

    const graphToolBar = this.compBox.getCompMergeName(
      this.compID,
      `graphToolbar`
    );
    switch (code) {
      case keyboard.del:
        graphToolBar.handleDelete();
        break;
      case keyboard.back:
        graphToolBar.handleDelete();
        break;
      case keyboard.ctrlY:
        graphToolBar.handleRedo();
        break;
      case keyboard.ctrlZ:
        graphToolBar.handleUndo();
        break;
      default:
    }
  },
  onKeyUp() {
    this.keydown = false;
  },
  onCanvasMouseout(e) {
    // 离开画布时不允许键盘操作
    // isKeyboardEnabled在initBehavors()中定义
    this.isKeyboardEnabled = false;
  },
  onCanvasMouseenter(e) {
    // 进入画布时才可以允许键盘操作
    this.isKeyboardEnabled = true;
  }
};
